package com.runfast.waimai.web.controller.goods;

import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastGoodsSellService;
import com.runfast.waimai.service.RunfastGoodsSellStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * ${DESCRIPTION} 外卖商品规格
 *
 * @author chenzheng
 * @create 2018-05-08 19:56
 **/
@RestController("goodsSellStandardController")
@RequestMapping("/server/goodsSellStandard")
public class GoodsSellStandardController {
    @Autowired
    private RunfastGoodsSellStandardService runfastGoodsSellStandardService;
    @Autowired
    private RunfastGoodsSellService runfastGoodsSellService;
    /**
     * 商品分类列表带分页的
     */
   /* @GetMapping("/list")
    //@RequiresPermissions("server:goodsSellType:list")
    public Result list(@PageableDefault Pageable pageable, Map<String, Object> params){

        RunfastGoodsSellTypeExample example=new RunfastGoodsSellTypeExample();
        List<RunfastGoodsSellType> list=runfastGoodsSellTypeService.selectByExampleWithPageable(example,pageable);
        Integer total=runfastGoodsSellTypeService.countByExampleWithPageable(example,pageable);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }*/

    /**
     * 商家对应的---商品分类列表
     */
    /*@GetMapping("/select")
    public Result select(Integer businessId){
        if(businessId==null){return Result.fail(null,"商家id不能为空！");}
        RunfastBusiness business=runfastBusinessService.selectByPrimaryKey(businessId);
        RunfastGoodsSellTypeExample example=new RunfastGoodsSellTypeExample();
        example.createCriteria().andBusinessidEqualTo(business.getId()).andAgentidEqualTo(business.getAgentid()).andDeletedNotEqualTo(1);
        example.setOrderByClause("sort asc");
        List<RunfastGoodsSellType> list = runfastGoodsSellTypeService.selectByExample(example);
        return Result.ok(null,list);
    }*/

    /**
     * 保存商品分类
     */
    /*@PostMapping("/save")
    public Result save(@RequestBody RunfastGoodsSellType type){
        if(type==null){return Result.fail(null,"内容不能为空！");}
        if(type.getBusinessid()==null){return Result.fail(null,"商家id不能为空！");}
        RunfastBusiness business=runfastBusinessService.selectByPrimaryKey(type.getBusinessid());
        if(business==null){return Result.fail(null,"找不到对应的商家！");}
        type.setAgentid(business.getAgentid());
        type.setAgentname(business.getAgentname());
        type.setCreatetime(new Date());
        type.setDeleted(0);
        RunfastGoodsSellTypeExample example=new RunfastGoodsSellTypeExample();
        example.createCriteria().andAgentidEqualTo(business.getAgentid()).andBusinessidEqualTo(business.getId());
        Integer total=runfastGoodsSellTypeService.countByExample(example);
        if(total==0){
            type.setSort(0);
        }else{
            type.setSort(total);
        }
        runfastGoodsSellTypeService.insertSelective(type);
        return Result.ok(null);
    }*/

    /**
     * 修改商品规格
     */
   /* @PostMapping("/update")
    public Result update(@RequestBody RunfastGoodsSellType type){
        if(type==null){return Result.fail(null,"修改的商品分类信息不能为空！");}
        runfastGoodsSellTypeService.updateByPrimaryKeySelective(type);
        return Result.ok(null);
    }*/

    /**
     * 删除规格
     */
    @GetMapping("/delete")
    public Result delete(RunfastGoodsSellStandard sellStandard){
        if(sellStandard!=null && sellStandard.getId()!=null){
            RunfastGoodsSellStandard   sellStandard1=runfastGoodsSellStandardService.selectByPrimaryKey(sellStandard.getId());
            if(sellStandard1!=null){
                sellStandard1.setDeleted(true);
                runfastGoodsSellStandardService.updateByPrimaryKeySelective(sellStandard1);
            }else{return Result.fail(null,"此规格不存在！");}
        }else{
            return Result.fail(null,"规格id不能为空");
        }
        return Result.ok(null);
    }

   /**沽清，置满*/
    @PostMapping("/setNum")
    public Result  setNum(@RequestBody RunfastGoodsSellStandard sellStandard){
        if(sellStandard!=null && sellStandard.getId()!=null && sellStandard.getSetNumType()!=null){
            RunfastGoodsSellStandard   sellStandard1=runfastGoodsSellStandardService.selectByPrimaryKey(sellStandard.getId());
            if(sellStandard1!=null){
                sellStandard1.setSetNumType(sellStandard.getSetNumType());
                setStandard(sellStandard1);
            }
        }else if(sellStandard!=null &&sellStandard.getIds().size()>0 && sellStandard.getSetNumType()!=null){
            for(Integer in:sellStandard.getIds()){
                RunfastGoodsSell sell=runfastGoodsSellService.selectByPrimaryKey(in);
                if(sell!=null){
                    RunfastGoodsSellStandardExample example=new RunfastGoodsSellStandardExample();
                    example.createCriteria().andDeletedNotEqualTo(true).andBusinessidEqualTo(sell.getBusinessid()).andGoodssellidEqualTo(sell.getId());
                     List<RunfastGoodsSellStandard> standards=runfastGoodsSellStandardService.selectByExample(example);
                     if(standards.size()>0){
                         for(RunfastGoodsSellStandard standard:standards){
                             RunfastGoodsSellStandard   sellStandard1=runfastGoodsSellStandardService.selectByPrimaryKey(standard.getId());
                            if(sellStandard1!=null){
                                sellStandard1.setSetNumType(sellStandard.getSetNumType());
                                setStandard(sellStandard1);
                            }
                         }
                     }
                }
            }
        }
      return Result.ok(null);
    }
    private void setStandard(RunfastGoodsSellStandard sellStandard){
        if(sellStandard!=null && sellStandard.getId()!=null && sellStandard.getSetNumType()!=null){
            if(sellStandard.getSetNumType()!=null &&sellStandard.getSetNumType()==1){
                //沽清
                sellStandard.setNum(0);
                runfastGoodsSellStandardService.updateByPrimaryKeySelective(sellStandard);
                RunfastGoodsSell goodsSell=new RunfastGoodsSell();
                goodsSell.setId(sellStandard.getGoodssellid());
                goodsSell.setBusinessid(sellStandard.getBusinessid());
                runfastGoodsSellService.updateNum(goodsSell);
            }else if(sellStandard.getSetNumType()!=null &&sellStandard.getSetNumType()==2){
                //置满
                sellStandard.setNum(10000);
                runfastGoodsSellStandardService.updateByPrimaryKeySelective(sellStandard);
                RunfastGoodsSell goodsSell=new RunfastGoodsSell();
                goodsSell.setId(sellStandard.getGoodssellid());
                goodsSell.setBusinessid(sellStandard.getBusinessid());
                runfastGoodsSellService.updateNum(goodsSell);
            }
        }
    }


    /**批量处理，规格 沽清，置满，餐盒费，价格*/
    @PostMapping("/batchOperating")
    public Result batchOperating(@RequestBody  RunfastGoodsSellStandard sellStandard ){
        if(sellStandard!=null && sellStandard.getGoodsSellIds()!=null && sellStandard.getBusinessid()!=null && sellStandard.getGoodsSellIds().size()>0){
              for(Integer in:sellStandard.getGoodsSellIds()){
                  RunfastGoodsSellStandardExample example=new RunfastGoodsSellStandardExample();
                  example.createCriteria().andBusinessidEqualTo(sellStandard.getBusinessid()).andGoodssellidEqualTo(in).andDeletedNotEqualTo(true);
                  List<RunfastGoodsSellStandard> list=runfastGoodsSellStandardService.selectByExample(example);
                  if(list.size()>0){
                      for(RunfastGoodsSellStandard sellStandard1:list){
                          sellStandard.setId(sellStandard1.getId());
                          runfastGoodsSellStandardService.updateByPrimaryKeySelective(sellStandard);
                          RunfastGoodsSell goodsSell=new RunfastGoodsSell();
                          goodsSell.setId(sellStandard.getGoodssellid());
                          goodsSell.setBusinessid(sellStandard.getBusinessid());
                          runfastGoodsSellService.updateNum(goodsSell);
                      }
                  }
              }
        }
        return Result.ok(null);
    }
}
